package com.lai.shop.strategy.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lai.shop.config.Oauth2GiteeConfigProperties;
import com.lai.shop.config.Oauth2WeiboConfigProperties;
import com.lai.shop.dto.SocialTokenDto;
import com.lai.shop.dto.SocialUserInfoDto;
import com.lai.shop.enums.LoginTypeEnum;
import com.lai.shop.enums.StatusCodeEnum;
import com.lai.shop.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.lai.shop.constant.SocialLoginConstant.*;

/**
 * @BelongsPackage: com.lai.shop.strategy.impl
 * @Author: laiciqi
 * @CreateTime: 2022-05-23  17:17
 * @Description: gitee社交登录策略
 * @Version: 1.0
 */
@Service
public class GiteeSocialLoginStrategyImpl extends AbstractSocialLoginStrategyImpl {

    @Autowired
    Oauth2GiteeConfigProperties giteeConfigProperties;

    /**
     * 获取三方令牌
     *
     * @param data 数据
     * @return
     */
    @Override
    public SocialTokenDto getSocialToken(String data) {
        //获取令牌   需要的参数
        HashMap<String, Object> accessTokenMap = new HashMap<>();
        accessTokenMap.put(CLIENT_ID, giteeConfigProperties.getClientId());
        accessTokenMap.put(CLIENT_SECRET, giteeConfigProperties.getClientSecret());
        accessTokenMap.put(GRANT_TYPE, giteeConfigProperties.getGrantType());
        accessTokenMap.put(REDIRECT_URI, giteeConfigProperties.getRedirectUri());
        accessTokenMap.put(CODE, data);
        try {
            //发送请求获取accesstoken
            String accessTokenJsonMap = HttpUtil.post(giteeConfigProperties.getAccessTokenUrl(), accessTokenMap);
            Gson gson = new Gson();
            //把json转换成map
            Map<String, String> accessToken = gson.fromJson(accessTokenJsonMap, new TypeToken<Map<String, String>>() {
            }.getType());

            return SocialTokenDto.builder()
                    .openId(accessToken.get("uid"))
                    .accessToken(accessToken.get("access_token"))
                    .build();
        } catch (Exception e) {
            throw new BizException(StatusCodeEnum.GITEE_LOGIN_ERROR);
        }

    }

    @Override
    public SocialUserInfoDto getSocialUserInfo(SocialTokenDto socialTokenDto) {
        //根据获取access_token和 uid获取用户的详细信息
        Map<String, Object> userInfoQueryMap = new HashMap<>();
        userInfoQueryMap.put(ACCESS_TOKEN, socialTokenDto.getAccessToken());
        try {
            //发送get请求获取用户信息
            String body = HttpRequest.get(giteeConfigProperties.getUserInfoUrl()).form(userInfoQueryMap).execute().body();

            Gson gson = new Gson();
            Map<String, String> userJsonMap = gson.fromJson(body, new TypeToken<Map<String, String>>() {
            }.getType());

            return SocialUserInfoDto.builder()
                    .openId(userJsonMap.get("id"))
                    .nickname(userJsonMap.get("name"))
                    .avatar(userJsonMap.get("avatar_url"))
                    .loginType(LoginTypeEnum.GITEE.getType())
                    .build();
        } catch (Exception e) {
            throw new BizException(StatusCodeEnum.GITEE_LOGIN_ERROR);
        }

    }
}
